home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / ctop12b.zip / SIEVE.C < prev    next >
C/C++ Source or Header  |  1989-02-28  |  609b  |  37 lines

  1. /*
  2.     Sieve of Erastothenes
  3. */
  4.  
  5. #define UPTO        8192
  6. #define ITERATIONS    10
  7. #define YES        1
  8. #define NO        0
  9.  
  10. int    isprime[UPTO];
  11.  
  12. main()
  13. {
  14.     int    i, j,
  15.         iterate;
  16.     int    count;
  17.  
  18.     printf("Start...\7\n");
  19.     for (iterate = 0; iterate < ITERATIONS; iterate++) {
  20.         count = 0;
  21.         
  22.         /* initialise array to 'all prime' */
  23.         for (i = 0; i < UPTO; isprime[i++] = YES);
  24.  
  25.         for (i = 2; i < UPTO; i++)
  26.             if (isprime[i]) {
  27.                 count++;
  28.                 /* 'cross out' all multiples of i: */
  29.                 for (j = 2*i; j < UPTO; j += i)
  30.                     isprime[j] = NO;
  31.             }
  32.     }
  33.     printf("Finish...\7 Found %d primes.\n", count);
  34. }
  35.  
  36.  
  37.